
/**************************************************START OF FILE*****************************************************/





/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "xBSP_Pwm.h"



/*------------------------------------------------------------------------------------------------------------------
Macros
*/


/*------------------------------------------------------------------------------------------------------------------
Variables
*/
TIM_HandleTypeDef TIM2_Handler;         //定时器2PWM句柄
TIM_OC_InitTypeDef TIM2_CHnHandler;	    //定时器2句柄




/*
********************************************************************************************************************
@ Brief  : pwm init

@ Param  : NONE

@ Return : NONE

@ Author : lyc

@  Date  : 2020 - 04 - 02
********************************************************************************************************************
*/
void xBSP_PWM_Init(uint16_t arr, uint16_t psc)
{
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_TIM2_CLK_ENABLE();				//使能定时器2
    __HAL_RCC_GPIOA_CLK_ENABLE();				//开启GPIOA时钟

    GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1; //PA0.1
    GPIO_Initure.Mode = GPIO_MODE_AF_PP;  		//复用推挽输出
    GPIO_Initure.Pull = GPIO_PULLUP;        	//上拉
    GPIO_Initure.Speed = GPIO_SPEED_HIGH;   	//高速
    GPIO_Initure.Alternate = GPIO_AF1_TIM2;		//PA1复用为TIM2_CH1.CH2
    HAL_GPIO_Init(GPIOA, &GPIO_Initure);

    TIM2_Handler.Instance = TIM2;          				//定时器2
    TIM2_Handler.Init.Prescaler = psc;     				//定时器分频
    TIM2_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; //向上计数模式
    TIM2_Handler.Init.Period = arr;        				//自动重装载值
    TIM2_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    HAL_TIM_PWM_Init(&TIM2_Handler);       				//初始化PWM

    TIM2_CHnHandler.OCMode = TIM_OCMODE_PWM1; 			//模式选择PWM1
    TIM2_CHnHandler.Pulse = arr / 2;       				//设置比较值,此值用来确定占空比，默认比较值为自动重装载值的一半,即占空比为50%
    TIM2_CHnHandler.OCPolarity = TIM_OCPOLARITY_HIGH; 	//输出比较极性为低

    HAL_TIM_PWM_ConfigChannel(&TIM2_Handler, &TIM2_CHnHandler, TIM_CHANNEL_1); //配置TIM2通道1
    //HAL_TIM_PWM_Start(&TIM2_Handler, TIM_CHANNEL_1); 	//开启PWM通道1

    HAL_TIM_PWM_ConfigChannel(&TIM2_Handler, &TIM2_CHnHandler, TIM_CHANNEL_2); //配置TIM2通道2
    //HAL_TIM_PWM_Start(&TIM2_Handler, TIM_CHANNEL_2); 	//开启PWM通道2

    HAL_TIM_PWM_Stop(&TIM2_Handler, TIM_CHANNEL_1);
    HAL_TIM_PWM_Stop(&TIM2_Handler, TIM_CHANNEL_2);

    TIM2->CCR1 = 500;
    TIM2->CCR2 = 0;
}

void xBSP_PWM_Start(void)
{
    HAL_TIM_PWM_Start(&TIM2_Handler, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&TIM2_Handler, TIM_CHANNEL_2);
}

void xBSP_PWM_Stop(void)
{
    HAL_TIM_PWM_Stop(&TIM2_Handler, TIM_CHANNEL_1);
    HAL_TIM_PWM_Stop(&TIM2_Handler, TIM_CHANNEL_2);
}

/****************************************************END OF FILE*****************************************************/
